package nc.ui.med.pub.query;

import nc.ui.pub.beans.UIRefPane;
import nc.ui.querytemplate.QueryConditionEditorContext;
import nc.ui.querytemplate.filtereditor.CheckBoxFilterEditor;
import nc.ui.querytemplate.filtereditor.IFilterEditor;
import nc.ui.querytemplate.filtereditor.IFilterEditorFactory;
import nc.ui.querytemplate.meta.FilterMeta;
import nc.ui.querytemplate.meta.IFilterMeta;
import nc.ui.querytemplate.valueeditor.DefaultFieldValueEditor;
import nc.ui.querytemplate.valueeditor.IFieldValueElementEditor;

@SuppressWarnings( { "unchecked", "serial", "deprecation", "unused",
		"static-access" })
public class MedDefaultFilterEditorFactory implements IFilterEditorFactory {

	private QueryConditionBaseDLG qrydlg;

	public MedDefaultFilterEditorFactory(QueryConditionEditorContext con,
			QueryConditionBaseDLG dlg) {
		super();
		qrydlg = dlg;
	}

	public IFilterEditor createFilterEditor(IFilterMeta meta) {
		MedQueryFieldAddInfo info = qrydlg.getMedTempInfo()
				.getSCMQueryFieldAddInfo(meta.getFieldCode());
		if (info != null && info.getCheckBoxEditorDatas() != null
				&& info.getCheckBoxEditorDatas().length > 0) {

			CheckBoxFilterEditor checkBoxEditor = new CheckBoxFilterEditor(
					meta, info.getCheckBoxEditorDatas());
			return checkBoxEditor;

		}
		if (qrydlg.getMedTempInfo().getFreeinfo() != null
				&& qrydlg.getMedTempInfo().getFreeinfo().length > 0
				&& qrydlg.getMedTempInfo().getFreeinfo()[0].equals(meta
						.getFieldCode())) {
			return new FreeFilterEditor(getQrydlg().getQueryContext(),
					(FilterMeta) meta, getQrydlg(), qrydlg.getMedTempInfo()
							.getFreeinfo()[1]);
		}

		if (qrydlg.getMedTempInfo().getBatchinfo() != null
				&& qrydlg.getMedTempInfo().getBatchinfo().length > 0
				&& qrydlg.getMedTempInfo().getBatchinfo()[0].equals(meta
						.getFieldCode())) {
			return new BatchFilterEditor(getQrydlg().getQueryContext(),
					(FilterMeta) meta, getQrydlg(), qrydlg.getMedTempInfo()
							.getBatchinfo()[1]);
		}

		MedDefaultFilterEditor editor = new MedDefaultFilterEditor(qrydlg
				.getQueryContext(), meta, qrydlg);
		editor.addActionListener(qrydlg.getFieldValueEditorListener());
		qrydlg.getMedTempInfo().addRefEditor(editor);
		// qrydlg.afterAddFilterEditor(editor);

		qrydlg.getMedTempInfo().addToInitFilterEditorList(editor);

		if (qrydlg.getMedTempInfo().getPowerDefaultCorpField() != null
				&& qrydlg.getMedTempInfo().getPowerDefaultPkValues() != null
				&& qrydlg.getMedTempInfo().getPowerDefaultPkValues().size() > 0
				&& qrydlg.getMedTempInfo().getPowerDefaultCorpField().equals(
						meta.getFieldCode())) {

			IFieldValueElementEditor valueedit = ((DefaultFieldValueEditor) editor
					.getFieldValueEditor()).getFieldValueElemEditor();

			String swhere = " ( ";
			boolean b = false;
			for (String pk : qrydlg.getMedTempInfo().getPowerDefaultPkValues()) {
				if (b)
					swhere += ", ";
				swhere += "'" + pk + "'";
				b = true;
			}
			swhere += " ) ";
			swhere = "pk_corp in " + swhere;
			// valueedit.setValues(null);
			if (valueedit.getFieldValueElemEditorComponent() != null
					&& (valueedit.getFieldValueElemEditorComponent() instanceof UIRefPane)) {
				((UIRefPane) valueedit.getFieldValueElemEditorComponent())
						.setWhereString(swhere);

				MedDefaultFilter fl = QueryDlgUtil.getSCMDefaultFilter(
						(FilterMeta) meta, QueryDlgUtil.getRefValueObject(
								qrydlg.getMedTempInfo()
										.getPowerDefaultPkValues().get(0), ""),
						qrydlg);
				editor.setFilter(fl);
			}
		}

		return editor;
	}

	public QueryConditionBaseDLG getQrydlg() {
		return qrydlg;
	}

}
